package com.lineage.data.item_etcitem.add;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.ExtraAttrWeaponTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ItemStatus;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1AttrWeapon;
import com.lineage.server.utils.log.PlayerLogUtil;
import java.sql.Timestamp;
import java.util.Random;

public class AttrWeaponScroll2 extends ItemExecutor {
  private static final Random _random = new Random();
  
  private int _attr_id;
  
  private int _type;
  
  public static ItemExecutor get() {
    return new AttrWeaponScroll2();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int targObjId = data[0];
    L1ItemInstance tgItem = pc.getInventory().getItem(targObjId);
    if (tgItem == null)
      return; 
    if (tgItem.getItem().getType2() != 1) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
    if (tgItem.getBless() >= 128) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
    int attrEnchantKind = tgItem.getAttrEnchantKind();
    int attrEnchantLevel = tgItem.getAttrEnchantLevel();
    if (attrEnchantKind != this._attr_id)
      attrEnchantLevel = 0; 
    L1AttrWeapon attrWeapon = ExtraAttrWeaponTable.getInstance().get(this._attr_id, attrEnchantLevel + 1);
    if (attrWeapon == null || attrWeapon.getChance() <= 0) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("已达该属性最高阶级。"));
      return;
    } 
    pc.getInventory().removeItem(item, 1L);
    if (_random.nextInt(100) < attrWeapon.getChance()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(1410, tgItem.getLogName()));
      tgItem.setAttrEnchantKind(this._attr_id);
      tgItem.setAttrEnchantLevel(attrEnchantLevel + 1);
      属性卷轴成败纪录("玩家 :" + pc.getName() + "使用 " + item.getName() + "(升)至Lv" + tgItem.getAttrEnchantLevel() + " ,时间:" + 
          new Timestamp(System.currentTimeMillis()) + ")");
    } else if (attrWeapon.gettype() == 0) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("强化失败,没发生什么事情。"));
    } else if (attrWeapon.gettype() == 1) {
      if (attrEnchantLevel > 0) {
        tgItem.setAttrEnchantLevel(attrEnchantLevel - 1);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("强化失败,倒退-1。"));
      } 
      if (attrEnchantLevel == 0) {
        tgItem.setAttrEnchantKind(0);
        tgItem.setAttrEnchantLevel(0);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("强化失败,没发生什么事情。"));
      } 
      pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
      pc.getInventory().saveItem(tgItem, 4);
    } else if (attrWeapon.gettype() == 2) {
      pc.getInventory().removeItem(tgItem, 1L);
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("强化失败,并删除强化物品。"));
    } else if (attrWeapon.gettype() == 3) {
      tgItem.setAttrEnchantKind(0);
      tgItem.setAttrEnchantLevel(0);
      pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
      pc.getInventory().saveItem(tgItem, 4);
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("强化失败,该装倍武器属性归0。"));
    } else {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("强化失败,没发生什么事情。"));
    } 
    pc.getInventory().updateItem(tgItem, 3072);
    pc.getInventory().saveItem(tgItem, 3072);
  }
  
  public static void 属性卷轴成败纪录(String info) {
    PlayerLogUtil.writeLog("[记录]-武器属性卷轴", info);
  }
  
  public void set_set(String[] set) {
    try {
      this._attr_id = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
    try {
      this._type = Integer.parseInt(set[2]);
    } catch (Exception exception) {}
  }
}
